package eddie;

public class Algorithms {

    private static final String NUM = "0123456789abcdefghijklmnopqrstuvwxyz";

    public static String changeBase(String input, int base1, int base2) {
        input = input.toLowerCase();
        String baseChars = "";
        String output = "";
        int decInt = 0;

        for (int i = 0; i < base1; i++) {
            baseChars += NUM.charAt(i);
        }

        for (int i = 0; i < input.length(); i++) {
            if (!baseChars.contains(input.charAt(i) + "")) {
                return "Not a valid number";
            } else {
                decInt += Math.pow(base1, input.length() - 1 - i) * baseChars.indexOf(input.charAt(i) + "");
            }

        }

        baseChars = "";

        for (int i = 0; i < base2; i++) {
            baseChars += NUM.charAt(i);
        }

        while (decInt > 0) {
            output = baseChars.charAt(decInt % base2) + output;
            decInt /= base2;
        }

        return output;
    }

    public static String primeFactor(double input) {
        String output = "";
        int count;

        increment:
        for (int increment = 2; increment < Math.pow(input, 2); increment++) {
            count = 0;

            while (input % increment == 0) {
                for (int divisor = 2; divisor < Math.sqrt(increment); divisor++) {
                    if (increment % divisor == 0) {
                        continue increment;
                    }
                }
                count++;
                input = input / increment;
            }

            if (count == 1) {
                output += "*" + increment;
            }

            if (count > 1) {
                output += "*" + increment + "^" + count;
            }

        }
        return output.substring(1);
    }
}
